package com.example.nio.commons;

import io.netty.channel.Channel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class User {
    public static Map<String,Channel> ipChannel = new Hashtable<>();//ip-通道
    // 添加用户
    public static void addUser(String toUserName, Channel channel) {
        ipChannel.put(toUserName, channel);
    }
    // 删除通道
    public static boolean removeUserChannel(String toUserName) {
        Channel remove = ipChannel.remove(toUserName);
        return remove == null ? false : true;
    }

    // 添加用户
    public static boolean hasChannel(String ipAddress) {
        for (String ip : ipChannel.keySet()) {
            if (ip.equals(ipAddress)){
                return true;
            }
        }
        return false;
    }


    public static Channel getChannelByIp(String ipAddress){
        return User.ipChannel.get(ipAddress);
    }
}
